package cn.jiyun;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.HashMap;

//Mongo入门Demo
public class MongoAdd {
    //查询所有
    public static void main(String[] args) {
        //创建连接
        MongoClient client=new MongoClient("127.0.0.1");
        //打开数据库
        MongoDatabase spitdb = client.getDatabase("spitdb");
        //获取集合
        MongoCollection<Document> spit = spitdb.getCollection("spit");

        HashMap<String, Object> map = new HashMap();
        map.put("name","范冰");
        map.put("userid","1003");
        map.put("visits",3333);

        Document document=new Document(map);
        //插入数据
        spit.insertOne(document);
        //关闭连接
        client.close();
    }

}
